<!--职业安全管理现状普查表模版-->
<template>
  <Layout>
    <Form ref="queryForm" :model="dataToQuery" :label-width="120">
      <Row class="query-form">
        <Col span="8">
          <FormItem label='评价项目：'>
            <Input  class='' placeholder="请填写评价项目" type='text' v-model='dataToQuery.evaluateItem'></Input>
          </FormItem>
        </Col>
        <Col span="8">


        </Col>


        <Col  span="8" v-if="!switchFlag">
<!--          <Button @click="switchQuery(true)" class="ivu-btn-switch" type="text">展开∨</Button>-->
          <Button @click="reset" class="ivu-btn-reset">重置</Button>
          <Button type="primary" class="ivu-btn-query" icon="md-search" @click="query">查询</Button>
        </Col>
        <Col span="8" v-else>
          <FormItem label='隐患分类：'>
            <Cascader  :data="areaTree" v-model="dataToQuery.dangerSortArr" change-on-select :transfer="true"
                      @on-change="(value,selectedData)=>{$set(dataToQuery,'dangerSort',value.join(','))}"></Cascader>
          </FormItem>
        </Col>
      </Row>
      <Row v-show="switchFlag" class="query-form">
        <Col span="8">
          <FormItem label='整改责任人：' prop='persionHeld'>
            <Input  class='' placeholder="请填写整改责任人" type='text' v-model='dataToQuery.persionHeld'></Input>
          </FormItem>
        </Col>


      </Row>
      <Row v-show="switchFlag" class="query-form">

        <Col span="8" :offset="16">
          <Button @click="switchQuery(false)" class="ivu-btn-switch" type="text">收起∧</Button>
          <Button @click="reset" class="ivu-btn-reset">重置</Button>
          <Button class="ivu-btn-query" icon="md-search" @click="query">查询</Button>
        </Col>
      </Row>
      <Row class="query-form">
        <Col span="24">
          <Button size="large" class="ivu-btn-add" icon="md-add" @click="addInfo">新增</Button>

        </Col>
      </Row>
    </Form>
    <Table ref='table' :height="tableHeight" no-data-text="没有查询到相关数据!" @on-selection-change='getSelectRows'
           :loading='pageStatus.loading ||  pageStatus.baseLoading'
           border :columns="columns1" :data="dataTable">
    </Table>
    <Row style="margin-right:10px;">
      <Col span="24">
        <Page style="float: right" :total="pagedata.total" :page-size="pagedata.pageSize" @on-change="pageChange"
              @on-page-size-change="pageSizeChange" :page-size-opts="pageSizeArray" show-total show-sizer
              show-elevator/>
      </Col>
    </Row>
    <Modal :mask-closable="false" :title="mode == 'view' ?'职业安全管理现状普查表详情':'职业安全管理现状普查表'" :transfer=false v-model="dialogShow" v-if="dataToView && dialogShow"
           :width=700 :styles="{height:'300px'}" class-name='limit-height-dialog'>
      <Row>
        <Col span="24">
          <Form ref="dataToView" :class="mode === 'view'?'modal-view-detail':'modal-form'" :show-message="false"
                :model="dataToView" :rules="mode === 'view'?{} :ruleCustom"
                :label-position="mode === 'view'?'right':'top'" :label-width="mode === 'view'?160:null">
            <Row type="flex" justify="space-around" class="code-row-bg">
              <Col span="23">
                <FormItem label='评价项目：' prop='evaluateItem'>
                  <div v-if="mode === 'view'">{{ dataToView.evaluateItem }}</div>
                  <Input v-else class=''placeholder="请填写评价项目" type='text' v-model='dataToView.evaluateItem'></Input>
                </FormItem>
              </Col>

            </Row>
            <Row type="flex" justify="space-around" class="code-row-bg">

              <Col span="23">
                <FormItem label='评价内容：' prop='evaluationContent'>
                  <div v-if="mode === 'view'">{{ dataToView.evaluationContent }}</div>
                  <Input v-else class=''placeholder="请填写评价内容" type='textarea' v-model='dataToView.evaluationContent'></Input>
                </FormItem>
              </Col>
            </Row>


          </Form>
        </Col>
      </Row>
      <div slot="footer">
        <Button v-if="mode != 'view'" type="primary" class='ivu-btn-save' @click="submit('dataToView')">确定</Button>
        <Button v-if="mode != 'view'" type="primary" class='ivu-btn-cancel' @click="handleCancel">取消</Button>
        <Button v-if="mode === 'view'" class='ivu-btn-save' @click="dialogShow=false">关闭</Button>
      </div>
    </Modal>
  </Layout>
</template>

<script>
import {Api} from '@/api/index.js'
import {sessionStorage} from '@/common/cacheUtils.js';
import {formatDate} from '@/tools/index'
import pageCommonFunction from '@/components/pageCommonFunction.js'
import dangerList from "@/views/safe/treeYearAction/DangerListMod.vue";

var cascaderDom = () => import('@/components/cascader-dom.vue');
export default {
  components: {
    cascaderDom,dangerList
  },
  data() {
    return {
      ...this.createCommonDataField(),
      switchFlag: false,
      uploadFile:null,
      buttonLoading1:false,
      importModal:false,
      reason:false,
      token:sessionStorage('token').get('Authorization'),
      user:sessionStorage('userId').getObject('userId'),
      columns1: [
        { title: '序号', width: 80, fixed: 'left', align: 'center', render: this.indexColumnsRender},
        { key:'evaluateItem',title: '评价项目',  align: 'center',minWidth: 120 },
        { key:'evaluationContent',title: '评价内容',  align: 'center',minWidth: 120,tooltip: true },
        {
          title: '操作',
          key: 'action',
          fixed: 'right',
          width: 160,
          align: 'center',
          render: (h, params) => {
            if (this.currentUser.loginId === this.Common.admin || this.currentUser.userId === params.row.createUserId  || this.currentUser.userRole.find(obj => obj.roleId == '73b38b37dff64392ba0ee7dd579bf293') != undefined
                ||sessionStorage('userId').getObject('userId').userBeHandedOver.find(obj => obj == params.row.createUserId) != undefined) {
              return h('div', [

                h('A', {
                  props: {type: 'primary', size: 'small'},
                  style: {fontSize: '14px',},
                  on: {
                    click: () => {
                      this.viewData(params);
                    }
                  }
                }, '详情'),
                h('Divider', {props: {type: 'vertical'}}, ''),
                h('A', {
                  props: {type: 'primary', size: 'small'},
                  disabled: true,
                  style: {fontSize: '14px'},
                  on: {
                    click: () => {
                      this.editData(params);
                    }
                  }
                }, '修改'),
                h('Divider', {props: {type: 'vertical'}}, ''),
                h('A', {
                  props: {type: 'primary', size: 'small'},
                  style: {fontSize: '14px', color: 'red'},
                  on: {
                    click: () => {
                      this.deleteData(params);
                    }
                  }
                }, '删除')
              ]);
            } else {
              return h('div', [
                h('A', {
                  props: {type: 'primary', size: 'small'},
                  style: {fontSize: '14px',},
                  on: {
                    click: () => {
                      this.viewData(params);
                    }
                  }
                }, '详情'),
                h('Divider', {props: {type: 'vertical'}}, ''),
                h('A', {
                  props: {type: 'primary', size: 'small'},
                  style: {fontSize: '14px', color: '#c5c8ce'}
                }, '修改'),
                h('Divider', {props: {type: 'vertical'}}, ''),
                h('A', {
                  props: {type: 'primary', size: 'small'},
                  style: {fontSize: '14px', color: '#c5c8ce'}
                }, '删除')
              ]);
            }
          }
        }
      ],
      upUrl:Api.uploadDangerList,
      ruleCustom: {
        evaluateItem: [ { required: true, message: '请填写评价项目', trigger: 'change',}],
        evaluationContent: [ { required: true, message: '请填写评价内容', trigger: 'change',}],

      },
      tableHeight: 10,
      crectification:[],
      // api
      standardSelectModal: false,
      queryUrl: Api.queryTemplateBasic,
      deleteUrl: Api.deleteTemplateBasic,
      updateUrl: Api.updateTemplateBasic,
      saveUrl: Api.saveTemplateBasic,
      buttonLoading: false,
      dangerModel:false,
    }
  },
  mounted() {
    let _this = this;
    this.dataToQuery = {};
    this.setTableHeight();
    this.query();
  },
  methods: {

    ...pageCommonFunction,
    renderCategoryType(h, params){
      let key = params.column.key;
      if (!params.row[key]) return "";

      if (params.row[key] == '1'){
        return h('span','第一次申报')
      }else{
        return h('span','变更申报')
      }



    },
    beforeEditDialogOpen(data){
      if (data.row.categoryType == '1'){
        this.reason = true;
      }else{
        this.reason = false;
      }
    },
    radioCategoryType(data){
      if (data == '2'){
        this.reason = false;
      }else{
        this.reason = true;
      }
    },
    openDangerModal(){
      this.dangerModel = true;
    },
    selectDanger(row){
      this.dangerModel = false;
      this.dataToView.dangerId = row.id;
      this.dataToView.dangerName = row.dangerName;
    },
    dictListToMap(arr, key = 'codingCodeId') {
      var map = {}
      arr.map(item => {
        map[item[key]] = item
      })
      return map
    },
    areaCodeToSortName(codeStr){
      if(!codeStr || codeStr === "") return "";
      var arr = codeStr.split(',')
      var outArr = []
      arr.forEach(codingCodeId => {
        if (this.areaMap[codingCodeId]) {
          outArr.push(this.areaMap[codingCodeId].codingName)
        }
      })
      return outArr.join("·")
    },
    download(params){
      this.buttonLoading = true
      this.$http.get(Api.downloadHealthBasic+params.row.id, {
        responseType: 'blob'      //告诉服务器我们需要的响应格式
      }).then((res) => {
        const aLink = document.createElement("a");
        let blob = new Blob([res.data], {
          type: res.bodyBlob.type      //将会被放入到blob中的数组内容的MIME类型
        });
        aLink.href = URL.createObjectURL(blob)
        aLink.download = "单位基本情况普查表(" + formatDate(new Date(), "yyyy-MM-dd") + ").xlsx" //标题
        aLink.click()
        document.body.appendChild(aLink)
        this.buttonLoading = false
      })
    },
    importDanger(){
      this.importModal = true;
    },

    uploadExcel(response, file, fileList){
      //上传完成后执行代码
      if (response.status === 200) {
        this.$Message.success('导入成功');
        this.query()
      } else if (response.status === 201) {
        this.$Message.error("导入失败");
        this.query()
      }
    },
    deleteData(params) {
      this.deleteDateAjax(params);
    },
    switchQuery(flag) {
      this.switchFlag = flag;
      if (!this.switchFlag) {
        this.tableHeight = this.tableHeight + 44;

      } else {
        this.tableHeight = this.tableHeight - 44;
      }

    },
    setOrgId(data) {
      this.dataToQuery.companyId = data[data.length - 1];
    },
    setOrgName(data) {

    },
    query() {

      this.queryAjax()
    },

    submit(name) {

      this.submitAjax(name);
    },
    createEmptyDataToAdd() {
      this.reason = true;
      return {
        companyId: sessionStorage('userId').getObject('userId').companyId,
        companyName: sessionStorage('userId').getObject('userId').companyName,
        categoryType:'1',
        actualPersionNum:0,
        actualWorkNum:0,
        actualWorkWomanNum:0,
        exposureHazardousNum:0,
        exposureHazardousWomanNum:0,
        hazardousNum:0,
        hazardCategoryNum:0,
        hazardFactorsNum:0,
        createUserName:this.user.userName,
        userPhone:this.user.mobileNumber
      }
    },
    downloadFile() {
      this.buttonLoading = true
      this.$http.get(Api.downloadMeasuresList, {
        params: this.dataToQuery,
        responseType: 'blob'      //告诉服务器我们需要的响应格式
      }).then((res) => {
        const aLink = document.createElement("a");
        let blob = new Blob([res.data], {
          type: res.bodyBlob.type      //将会被放入到blob中的数组内容的MIME类型
        });
        aLink.href = URL.createObjectURL(blob)
        aLink.download = "措施清单(" + formatDate(new Date(), "yyyy-MM-dd") + ").xlsx" //标题
        aLink.click()
        document.body.appendChild(aLink)
        this.buttonLoading = false
      })
    },
  },
  watch: {},

}
</script>

<style lang='css' scoped>
@import '../../../../index.css';

.importDanger{
  display:inline-block
}
</style>
